package game.base;

import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Point;
import android.view.MotionEvent;

public class BaseButton {
	
	Point location;
	ImagePlus ImgBackground;
	ImagePlus ImgBackgroundOn;
	Paint paint;
	int timeCollided = 0;
	UIButtonClick actionButtonClick;
	public boolean colide = false;
	MotionEvent holder;
	public BaseButton(Point location_, ImagePlus ImgBackground_, ImagePlus ImgBackgroundOn_){
		paint = new Paint();
		location = location_;
		ImgBackground = ImgBackground_;
		ImgBackgroundOn=ImgBackgroundOn_;
	}
	
	public void setActionClick(UIButtonClick click) {
		actionButtonClick = click;
	}

	public void draw(Canvas canvas){
		if(colide){
			canvas.drawBitmap(ImgBackgroundOn.getImage(), location.x, location.y, paint);	
		}else{
			canvas.drawBitmap(ImgBackground.getImage(), location.x, location.y, paint);	
		}
	}
	
	public void update(long difftime) {
		if (timeCollided > 0) {
			timeCollided -= difftime;
			if (timeCollided <= 0) {
				colide = false;
				if (actionButtonClick != null) {
					actionButtonClick.performActionClick(holder);
				}
			}
		}
		
	}
	
	public boolean eventHandler(MotionEvent event) {
		if (event.getX() > location.x && event.getX() < location.x + ImgBackground.getWidth() && event.getY() > location.y
				&& event.getY() < location.y + ImgBackground.getWidth()) {
			holder = event;
			colide = true;
			timeCollided = 200;
			return true;
		}
		return false;
	}

}
